package cn.iocoder.yudao.module.project.service.internalcoderecycle;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.internalcoderecycle.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.internalcoderecycle.InternalCodeRecycleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.internalcoderecycle.InternalCodeRecycleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 内码作废信息 Service 实现类
 *
 * @author 武汉华诚
 */
@Service
@Validated
public class InternalCodeRecycleServiceImpl implements InternalCodeRecycleService {

    @Resource
    private InternalCodeRecycleMapper internalCodeRecycleMapper;

    @Override
    public Long createInternalCodeRecycle(InternalCodeRecycleSaveReqVO createReqVO) {
        // 插入
        InternalCodeRecycleDO internalCodeRecycle = BeanUtils.toBean(createReqVO, InternalCodeRecycleDO.class);
        internalCodeRecycleMapper.insert(internalCodeRecycle);
        // 返回
        return internalCodeRecycle.getRecycleId();
    }

    @Override
    public void updateInternalCodeRecycle(InternalCodeRecycleSaveReqVO updateReqVO) {
        // 校验存在
        validateInternalCodeRecycleExists(updateReqVO.getRecycleId());
        // 更新
        InternalCodeRecycleDO updateObj = BeanUtils.toBean(updateReqVO, InternalCodeRecycleDO.class);
        internalCodeRecycleMapper.updateById(updateObj);
    }

    @Override
    public void deleteInternalCodeRecycle(Long id) {
        // 校验存在
        validateInternalCodeRecycleExists(id);
        // 删除
        internalCodeRecycleMapper.deleteById(id);
    }

    private void validateInternalCodeRecycleExists(Long id) {
        if (internalCodeRecycleMapper.selectById(id) == null) {
            throw exception(INTERNAL_CODE_RECYCLE_NOT_EXISTS);
        }
    }

    @Override
    public InternalCodeRecycleDO getInternalCodeRecycle(Long id) {
        return internalCodeRecycleMapper.selectById(id);
    }

    @Override
    public PageResult<InternalCodeRecycleDO> getInternalCodeRecyclePage(InternalCodeRecyclePageReqVO pageReqVO) {
        return internalCodeRecycleMapper.selectPage(pageReqVO);
    }

}